﻿using System;
using System.Text;

namespace base31.Hub.Helpers
{
    public static class Helper
    {
        /// <summary>
        /// Encodes a string as utf-8 data and returns an ascii string.
        /// </summary>
        /// <param name="data">The string data to encode.</param>
        /// <returns>An ascii string, or None if the 'data' parameter was None.</returns>
        public static string Utf8Encoded(string data)
        {
            return data;
        }

        /// <summary>
        /// Converts a URL (possibly containing unicode characters) to an Uri.
        /// </summary>
        /// <param name="url">String (normal or unicode) containing a URL, presumably having
        /// already been percent-decoded by a web framework receiving request
        /// parameters in a POST body or GET request's URL.</param>
        /// <returns>A properly encoded Uri</returns>
        public static Uri NormalizeIri(string url)
        {
            return new Uri(url);
        }

        /// <summary>
        /// Returns the sha1 hash of the supplied value.
        /// </summary>
        /// <param name="value">string to hash</param>
        /// <returns>Returns the sha1 hash string of the supplied value.</returns>
        public static string SHA1HashString(string value)
        {
            return value.GetSHA1HashToString();
        }

        /// <summary>
        /// Returns the sha1 hash of the supplied value.
        /// </summary>
        /// <param name="value">string to hash</param>
        /// <returns>Returns the sha1 hash int of the supplied value.</returns>
        public static int SHA1HashInt(string value)
        {
            return value.GetSHA1HashToInt();
        }

        /// <summary>
        /// Returns a valid entity key_name that's a hash of the supplied value.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetHashKeyName(string value)
        {
            return "hash_" + SHA1HashString(value);
        }

        /// <summary>
        /// Returns the sha1 hmac for a chunk of data and a secret.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public static string SHA1Hmac(string data, string secret)
        {
            return data.HMACSHA1ToString(Encoding.UTF8.GetBytes(secret));
        }

        /// <summary>
        /// Returns True if the URL is valid, False otherwise.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool IsValidUrl(string url)
        {
            return true;
        }
    }
}
